$(function () {
    let inputUtil = new Input_util();
    inputUtil.popUpsView();
    inputUtil.popUpsInsert()
    let popUpsDown = $(".pop-ups-down");
    let recycle = $(".recycle");
    let titleManagement = $(".title-management");
    popUpsDown.on("click", function () {
        $(".recycle-bin-window").css("display", "none")
    })
    recycle.on("click", function () {
        $(".recycle-bin-window").css("display", "block")
    })
    titleManagement.on("click", function () {
        let departments = new Departments();
        $(".table-body").empty();
        departments.getAllDepartments().then(res => {
            let departmentsJson = JSON.parse(res)
            for (let departments of departmentsJson) {
                console.log(departments)
                let row = `<div class="table-row">
                        <div class="table-cell">${departments.departmentId}</div>
                        <div class="table-cell" style="width: 15%">${departments.departmentName}</div>
                        <div class="table-cell" style="width: 0">
                            <button class="select">修改</button>   
                        </div>
                    </div>`
                $(".table-body").append(row)
            }
        })
        $(".bin-window").css("display", "block")
    })
    popUpsDown.on("click", function () {
        $(".bin-window").css("display", "none")
    })
    $(".search-img").on("click", function () {
        let val = $(".search-input input").val()
        window.location.href = "/www.xiaomi.com/background/home?menuId=103&employeeMc=" + val;
    })
    $(".select").on("click", function () {
        let departmentId = $("#lang").val()
        let administratorState = $("#status").val()
        window.location.href = "/www.xiaomi.com/background/home?menuId=103&departmentId=" + departmentId + "&administratorState=" + administratorState;
    })
    $("#date-flashback").on("click", function () {
        let date_entry = $("#date-flashback").val()
        window.location.href = "/www.xiaomi.com/background/home?menuId=103&dateEntry=" + date_entry;
    })
    $("#date-positive-order").on("click", function () {
        let date_entry = $("#date-positive-order").val()
        window.location.href = "/www.xiaomi.com/background/home?menuId=103&dateEntry=" + date_entry;
    })

    $(".new").on("click", function () {
        let account = $("#account").val()
        let password = $("#password").val()
        let values = []; // 存储被选中的复选框值

        $('.permissions:checked').each(function() {
            let value = $(this).val();
            values.push(value);
        });

        let permissions = values.join(','); // 使用逗号分隔值

        let name = $("#name").val()
        let phone = $("#phone").val()
        let mailbox = $("#mailbox").val()
        let spare_phone = $("#spare_phone").val()
        let home_address = $("#home_address").val()
        let contact = $("#contact").val()
        let signature = $("#signature").val()
        let contact_phone = $("#contact_phone").val()
        let gender = $('input[name=gender]:checked').val()
        let selectedFile = $("#file")[0].files

        let formData = new FormData();
        formData.append("image", selectedFile[0]);

        let date_birth = $("#date_birth").val()
        let addLang = $("#addLang").val()
        let administrator = new Administrator();
        administrator.getAdministratorId().then(res => {
            let administratorJson = JSON.parse(res);
            for (const administrator of administratorJson) {

            }
        })
        let administratorJson = {
            administratorId: account,
            password: password,
            permissions: permissions,
        }
        administrator.addAdministrator(administratorJson).then(res => {
            console.log(res)
            location.reload();
        })
        let administratorInformationJson = {
            administratorId: account,
            name: name,
            phone: phone,
            mailbox: mailbox,
            sparePhone: spare_phone,
            homeAddress: home_address,
            contact: contact,
            signature: signature,
            contactPhone: contact_phone,
            sex: gender,
            file: selectedFile,
            dateBirth: date_birth,
            departmentId: addLang
        }
        console.log(administratorInformationJson)
        // formData.append("administratorId", account);
        // formData.append("name", name);
        // formData.append("phone", phone);
        // formData.append("mailbox", mailbox);
        // formData.append("spare_phone", spare_phone);
        // formData.append("home_address", home_address);
        // formData.append("contact", contact);
        // formData.append("signature", signature);
        // formData.append("contact_phone", contact_phone);
        // formData.append("gender", gender);
        // formData.append("image", selectedFile[0]);
        // formData.append("date_birth", date_birth);
        // formData.append("departmentId", addLang);

        let administratorInformation = new Administrator_information();
        administratorInformation.addAdministratorInformation(administratorInformationJson).then(res => {
            console.log(res)
            location.reload();
        })


    })

    $(".cancel").on("click", function () {
        $(".add-window").css("display", "none")
    })


})